import CryptoJS from 'crypto-js'

// 时间戳 -- > 'yyyy-MM-dd hh:mm:ss'
export function formatDate(date,fmt) {
    if(/(y+)/.test(fmt)){
        fmt = fmt.replace(RegExp.$1,(date.getFullYear()+'').substr(4-RegExp.$1.length));
    }
    let o = {
        'M+':date.getMonth() + 1,
        'd+':date.getDate(),
        'h+':date.getHours(),
        'm+':date.getMinutes(),
        's+':date.getSeconds()
    };

    // 遍历这个对象
    for(let k in o){
        if(new RegExp(`(${k})`).test(fmt)){
        let str = o[k] + '';
        fmt = fmt.replace(RegExp.$1,(RegExp.$1.length===1)?str:padLeftZero(str));
        }
    }
    return fmt;
};


function padLeftZero(str) {
    return ('00'+str).substr(str.length);
}

 
const key = CryptoJS.enc.Utf8.parse("444235786425912D");  //十六位十六进制数作为密钥
const iv = CryptoJS.enc.Utf8.parse('99212547ABCF3412');   //十六位十六进制数作为密钥偏移量
// 解密
export function Decrypt(word) {
    let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
    let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
    let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
    return decryptedStr.toString();
}

// 加密
export function Encrypt(word) {
    let srcs = CryptoJS.enc.Utf8.parse(word);
    let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
    return encrypted.ciphertext.toString().toUpperCase();
}


export function bubbleSort(arr,type){
    for(var i=0;i<arr.length;i++){
        for(var j=0;j<arr.length-i-1;j++){
            if( type=='time' ){
                if( arr[j].minute == arr[j+1].minute ){
                    if( arr[j].second > arr[j+1].second ){
                        var swap = arr[j]
                        arr[j]=arr[j+1];
                        arr[j+1]=swap
                    }
                }

                if( arr[j].minute > arr[j+1].minute ){
                    var swap = arr[j]
                    arr[j]=arr[j+1];
                    arr[j+1]=swap
                }
            }else{
                if( arr[j][type] > arr[j+1][type] ){
                    var swap = arr[j]
                    arr[j]=arr[j+1];
                    arr[j+1]=swap
                }
            }
        }
    }
    arr.reverse()
}


let cached
export function getScrollBarSize(fresh) {
  if (fresh || cached === undefined) {
    const inner = document.createElement('div')
    inner.style.width = '100%'
    inner.style.height = '200px'

    const outer = document.createElement('div')
    const outerStyle = outer.style

    outerStyle.position = 'absolute'
    outerStyle.top = 0
    outerStyle.left = 0
    outerStyle.pointerEvents = 'none'
    outerStyle.visibility = 'hidden'
    outerStyle.width = '200px'
    outerStyle.height = '150px'
    outerStyle.overflow = 'hidden'

    outer.appendChild(inner)

    document.body.appendChild(outer)

    const widthContained = inner.offsetWidth
    outer.style.overflow = 'scroll'
    let widthScroll = inner.offsetWidth

    if (widthContained === widthScroll) {
      widthScroll = outer.clientWidth
    }

    document.body.removeChild(outer)

    cached = widthContained - widthScroll
  }
  return cached
}